/****************************************************************************
	This file is part of Basqet.

	Basqet is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Basqet is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
    along with Basqet.  If not, see <http://www.gnu.org/licenses/>.

****************************************************************************/


#include "textnotedata.h"
#include "xmlstrings.h"

#include "basketdescriptor.h"

#include <QDir>
#include <QBuffer>
#include <QList>

#include <QtDebug>



TextNoteData::TextNoteData(BasketDescriptor* container, BasketContent* content)
		:NoteData(container, content)
{
}


void TextNoteData::setup(QDomElement tag)
{
	NoteData::setup(tag);

	if (mBasket)
	{
		mDocumentFile = QFileInfo(QDir(mBasket->getBasketPath().filePath()), QString("%1.html").arg(getNoteID()));
		mHeaderFile = QFileInfo(QDir(mBasket->getBasketPath().filePath()), QString("%1_header.html").arg(getNoteID()));

		readFromHTML();
	}

	QDomElement	headerBodyLayout;

	if (0 == getObjectNode().elementsByTagName(XML_TEXTNOTE_HEADER_BODY_LAYOUT).count())
	{
		headerBodyLayout = getObjectNode().ownerDocument().createElement(XML_TEXTNOTE_HEADER_BODY_LAYOUT);
		getObjectNode().appendChild(headerBodyLayout);

		mHeaderBodySliderSizes = getObjectNode().ownerDocument().createAttribute(XML_TEXTNOTE_HEADER_BODY_SIZES_ATTR);

		// Set default-value, 0,20 -> no header and rest to the body.

		mHeaderBodySliderSizes.setValue("0,20");

		headerBodyLayout.setAttributeNode(mHeaderBodySliderSizes);

	}
	else
	{
		headerBodyLayout = getObjectNode().firstChildElement(XML_TEXTNOTE_HEADER_BODY_LAYOUT);
	}

	mHeaderBodySliderSizes  = headerBodyLayout.attributeNode(XML_TEXTNOTE_HEADER_BODY_SIZES_ATTR);


}

void TextNoteData::copyFrom(const NoteData* other)
{
	const TextNoteData* pOtherTextNote = dynamic_cast<const TextNoteData*>(other);

	if (other)
	{
		NoteData::copyFrom(other);
	}

	if (pOtherTextNote)
	{
		// Copying QTextDocument-objects seems to be a mess. We need to have the file
		// anyhow so lets write it to file and then read the file.
		QFile file(mDocumentFile.filePath());
		file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate);
		file.write(pOtherTextNote->mTextDocument.toHtml("utf-8").toUtf8());
		file.close();

		QFile headerFile(mHeaderFile.filePath());
		headerFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate);
		headerFile.write(pOtherTextNote->mHeaderDocument.toHtml("utf-8").toUtf8());
		headerFile.close();

		readFromHTML();


		setHeaderBodySliderSizes(pOtherTextNote->getHeaderBodySliderSizes());

	}

}

void TextNoteData::getContents(QMap<QString, QVariant>& content)
{
	NoteData::getContents(content);
	content.insert("headerDocument", QVariant(mHeaderDocument.toHtml("utf-8")));
	content.insert("bodyDocument", QVariant(mTextDocument.toHtml("utf-8")));

	content.insert("headerBodySlider", QVariant(mHeaderBodySliderSizes.value()));


}


void TextNoteData::setContents(const QMap<QString, QVariant>& content)
{
	NoteData::setContents(content);
	mHeaderDocument.setHtml(content["headerDocument"].toString());
	mTextDocument.setHtml(content["bodyDocument"].toString());

	mDocumentFile = QFileInfo(QDir(mBasket->getBasketPath().filePath()), QString("%1.html").arg(getNoteID()));
	mHeaderFile = QFileInfo(QDir(mBasket->getBasketPath().filePath()), QString("%1_header.html").arg(getNoteID()));

	saveToHTML();

	mHeaderBodySliderSizes.setValue(content["headerBodySlider"].toString());

	emit headerBodySliderResized(getHeaderBodySliderSizes());


}

bool TextNoteData::removeNote()
{
	qDebug() << "TextNoteData::removeNote";
	QFile file;

	file.remove(mDocumentFile.filePath());
	file.remove(mHeaderFile.filePath());

    return NoteData::removeNote();

}



void TextNoteData::readFromHTML()
{
	if (mDocumentFile.exists())
	{
		QFile file(mDocumentFile.filePath());
		file.open(QIODevice::ReadOnly | QIODevice::Text);
		mTextDocument.setHtml(QString().fromUtf8(file.readAll()));

		file.close();

	}

	if (mHeaderFile.exists())
	{
		QFile file(mHeaderFile.filePath());
		file.open(QIODevice::ReadOnly | QIODevice::Text);

		mHeaderDocument.setHtml(QString().fromUtf8(file.readAll()));

		file.close();

	}
}

void TextNoteData::saveToHTML()
{

	QFile file(mDocumentFile.filePath());
	file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate);
	file.write(mTextDocument.toHtml("utf-8").toUtf8());
	file.close();

	QFile headerFile(mHeaderFile.filePath());
	headerFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate);
	headerFile.write(mHeaderDocument.toHtml("utf-8").toUtf8());
	headerFile.close();
}


QTextDocument* TextNoteData::getDocument()
{
	return &mTextDocument;
}

QTextDocument* TextNoteData::getHeaderDocument()
{
	return &mHeaderDocument;
}


void TextNoteData::save()
{
	NoteData::save();
	saveToHTML();

}

void TextNoteData::setHeaderBodySliderSizes(QList<int> sizes)
{
	qDebug() << "Slider changed in TextNoteData:" << sizes;

	Q_ASSERT_X(sizes.count() == 2,
			   "TextNoteData::setHeaderBodySlider",
			   "THe Sizes-list from slider must contain exact 2 elements");

	QString	attr = QString("%1,%2").arg(sizes.at(0)).arg(sizes.at(1));

	mHeaderBodySliderSizes.setValue(attr);

	emit headerBodySliderResized(sizes);

}

QList<int> TextNoteData::getHeaderBodySliderSizes() const
{
	QList<int> result;

	QString attr = mHeaderBodySliderSizes.value();
	QStringList attrList = attr.split(",");

	foreach(QString value, attrList)
	{
		int iValue = value.toInt();
		result.append(iValue);
	}

	return result;

}
